import { getTopicById } from '~/server/database/services/topicService'

export default defineEventHandler(async (event) => {
  try {
    const id = getRouterParam(event, 'id')
    
    if (!id || isNaN(parseInt(id))) {
      throw createError({
        statusCode: 400,
        statusMessage: '无效的题目ID'
      })
    }

    // 获取题目详情
    const topic = await getTopicById(parseInt(id))

    if (!topic) {
      throw createError({
        statusCode: 404,
        statusMessage: '题目不存在'
      })
    }

    return {
      success: true,
      data: topic
    }
  } catch (error) {
    console.error('获取题目详情失败:', error)
    
    if (error && typeof error === 'object' && 'statusCode' in error) {
      throw error
    }
    
    throw createError({
      statusCode: 500,
      statusMessage: '获取题目详情失败'
    })
  }
})
